import { getToken } from '@/utils/user'
import routes from './path'

Vue.use(VueRouter)
console.log(routes)
const router = new VueRouter({
  mode: 'hash',
  routes: [
    ...routes,
    { // 输入错误路径，则默认走到第一个路由
      path: '*',
      redirect: routes[ 0 ].path
    }
  ],
})

/* 路由守卫 */
router.beforeEach((to, from, next) => {
  window.scrollTo(0, 0)

  if (to.meta.needLogin && !getToken()) {
    next('/')
  } else {
    next()
  }
})

export default router
